package incomeService

import (
	"fmt"
	time2 "hc_server/pkg/time"
	"hc_server/routers/models"
	"time"
)

type Income struct {
	AgentId  int
	TimeType int    //0=今日 1=昨日 2=本月 3=全部
	Time     string //时间检索
	OrderId  int    //订单id

	PageNum  int
	PageSize int
}

// GetIncomeInfo 获取收益信息
func (i *Income) GetIncomeInfo() (any, error) {
	//判断代理商是否存在
	_, err := models.AgentExistByID(i.AgentId)
	if err != nil {
		return nil, err
	}

	var timeSql string //0=今日 1=昨日 2=本月 3=全部
	var timeStr string
	switch i.TimeType {
	case 1:
		//昨日
		timeSql = fmt.Sprintf("created_at BETWEEN '%s' AND '%s'", time.Now().AddDate(0, 0, -1).Format("2006-01-02"), time.Now().Format("2006-01-02"))
		timeStr = time.Now().AddDate(0, 0, -1).Format("2006-01-02")
	case 2:
		//本月
		start, end := time2.GetMonthDateRange()
		timeSql = fmt.Sprintf("created_at BETWEEN '%s' AND '%s'", start, end)
		timeStr = time.Now().Format("2006-01")
	default:
		//今日
		timeSql = fmt.Sprintf("created_at BETWEEN '%s' AND '%s'", time.Now().AddDate(0, 0, 0).Format("2006-01-02"), time.Now().Format("2006-01-02"))
		timeStr = time.Now().Format("2006-01-02")
	}

	result, err := models.GetIncomeInfo(timeSql, timeStr, i.AgentId)
	if err != nil {
		return nil, err
	}

	return result, nil
}

// GetMerchantIncomeList 获取收益列表
func (i *Income) GetMerchantIncomeList() (any, error) {
	//判断代理商是否存在
	_, err := models.AgentExistByID(i.AgentId)
	if err != nil {
		return nil, err
	}

	result, err := models.GetMerchantIncomeList(i.AgentId, i.PageNum, i.PageSize)
	if err != nil {
		return nil, err
	}

	return result, nil
}

// GetMerchantIncomeDateList 获取收益列表
func (i *Income) GetMerchantIncomeDateList() (any, error) {
	//判断代理商是否存在
	_, err := models.AgentExistByID(i.AgentId)
	if err != nil {
		return nil, err
	}

	result, err := models.GetMerchantIncomeDateList(i.Time, i.AgentId)
	if err != nil {
		return nil, err
	}

	return result, nil
}

// GetMerchantIncomeDateInfo 收益商户订单详细
func (i *Income) GetMerchantIncomeDateInfo() (any, error) {
	//判断代理商是否存在
	_, err := models.AgentExistByID(i.AgentId)
	if err != nil {
		return nil, err
	}

	result, err := models.GetMerchantIncomeDateInfo(i.AgentId, i.OrderId)
	if err != nil {
		return nil, err
	}

	return result, nil
}
